package public

import (
	"encoding/json"
	"errors"
	"fmt"
	"net/http"
	"strings"
)

type WXLoginResp struct {
	OpenId     string `json:"openid"`
	SessionKey string `json:"session_key"`
	UnionId    string `json:"unionid"`
	ErrCode    int    `json:"errcode"`
	ErrMsg     string `json:"errmsg"`
	Token      string `json:"token"`
	Auth       uint32 `json:"auth"`
	Name       string `json:"name"`
	Phone      string `json:"phone"`
	Nickname   string `json:"nickname"`  //微信昵称
	Headimage  string `json:"headimage"` //微信头像
}
type PhoneInfo struct {
	PhoneNumber     string `json:"phoneNumber"`
	PurePhoneNumber string `json:"purePhoneNumber"`
}
type WXPhoneNumberResp struct {
	ErrCode   int    `json:"errcode"`
	ErrMsg    string `json:"errmsg"`
	PhoneInfo `json:"phone_info"`
}
type WXPhoneNumberInput struct {
	Code string `json:"code"`
}

// LoadUserOpen 获取用户的openId等信息
func LoadUserOpen(appId, secret, code string) (error, *WXLoginResp) {
	url := "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code"
	url = fmt.Sprintf(url, appId, secret, code)
	resp, err := http.Get(url)
	if err != nil {
		return errors.New("获取openId失败"), nil
	}
	defer resp.Body.Close()
	wxResp := WXLoginResp{}
	decoder := json.NewDecoder(resp.Body)
	if err := decoder.Decode(&wxResp); err != nil {
		return errors.New("解析微信返回失败"), nil
	}
	if wxResp.ErrCode != 0 {
		fmt.Println("错误", wxResp.ErrCode, wxResp.ErrMsg)
		return errors.New(wxResp.ErrMsg), nil
	}
	return nil, &wxResp
}
func LoadUserPhone(appId, secret, code string) (error, *WXPhoneNumberResp) {
	wx := &WxMessageReservation{
		AppId:  appId,
		Secret: secret,
	}
	token, err := wx.GetAccessToken()
	if err != nil {
		fmt.Println("这里的问题", err)
		return err, nil
	}
	fmt.Println("来到这里的了")
	body, err := json.Marshal(WXPhoneNumberInput{
		Code: code,
	})
	if err != nil {
		return err, nil
	}
	reqBody := strings.NewReader(string(body))
	resp, err := http.Post("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+token, "application/json;charset=utf-8", reqBody)
	if err != nil {
		fmt.Println(err, "这里的问题445555")
		return errors.New("获取手机号失败"), nil
	}
	defer resp.Body.Close()
	wxResp := WXPhoneNumberResp{}
	decoder := json.NewDecoder(resp.Body)
	if err := decoder.Decode(&wxResp); err != nil {
		return errors.New("解析微信返回失败"), nil
	}
	if wxResp.ErrCode != 0 {
		fmt.Println(wxResp.ErrCode, "这里的问题4444", token)
		return errors.New(wxResp.ErrMsg), nil
	}
	return nil, &wxResp
}
func LoadMessageOpen(appId, secret, code string) (error, *WXLoginResp) {
	url := "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code"
	url = fmt.Sprintf(url, appId, secret, code)
	resp, err := http.Get(url)
	if err != nil {
		return errors.New("获取openId失败"), nil
	}
	defer resp.Body.Close()
	wxResp := WXLoginResp{}
	decoder := json.NewDecoder(resp.Body)
	if err := decoder.Decode(&wxResp); err != nil {
		return errors.New("解析微信返回失败"), nil
	}
	if wxResp.ErrCode != 0 {
		return errors.New(wxResp.ErrMsg), nil
	}
	return nil, &wxResp
}
